package com.sgb.kjwl.view.ui.activity.vehicleManagement;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityDriverCertificationAuditStatusBinding;
import com.sgb.kjwl.di.vehicleManagement.DaggerDriverCertificationAuditStatusComponent;
import com.sgb.kjwl.di.vehicleManagement.DriverCertificationAuditStatusModule;
import com.swgk.core.view.entity.DriverAuthDetailEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.viewmodel.vehicleManagement.DriverCertificationAuditStatusAtvModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import java.util.List;

import javax.inject.Inject;

/**
 * 司机认证审核状态
 * status  这个是审核状态 0 待审核 1 审核通过 2 审核不通过
 * state   0  下架  1上架
 */

public class DriverCertificationAuditStatusActivity extends AppActivity {

    @Inject
    DriverCertificationAuditStatusAtvModel viewModel;

    ActivityDriverCertificationAuditStatusBinding binding;

    private String carLenNO = "";
    private String carTypeNO = "";
    private List<GoodsInfoTypeEntity3> carsLengthData;
    private List<GoodsInfoTypeEntity3> carsTypeData;
    private GoodsInfoTypeEntity3 goodsInfoTypeEntity3 = new GoodsInfoTypeEntity3();
    private int carLengthTypePosition;
    private int carTypePosition;

    public static void start(FragmentActivity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, DriverCertificationAuditStatusActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerDriverCertificationAuditStatusComponent.builder().appComponent(appComponent).driverCertificationAuditStatusModule(new DriverCertificationAuditStatusModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_driver_certification_audit_status);
        binding.setViewModel(viewModel);
        binding.include.ivBack.setOnClickListener(view -> finish());
        binding.include.titleView.setText("司机认证审核状态");
        binding.include.rightView.setText("修改审核信息");
        binding.include.rightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.linearLayout.setVisibility(View.VISIBLE);
                binding.tvName.setEnabled(true);
                binding.tvId.setEnabled(true);
                binding.tvCarNum.setEnabled(true);
                binding.tvPosition.setEnabled(true);
                binding.tvCarLengh.setEnabled(true);
                binding.tvCarModel.setEnabled(true);
                binding.tvCarWeight.setEnabled(true);

            }
        });

        binding.nextView.setOnClickListener(view -> sumbit());
        binding.tvCarWeight.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
        binding.tvCarLengh.setOnClickListener(view -> {
            hideKeyboard();
            showCarLengthTypePop();
        });

        binding.tvCarModel.setOnClickListener(view -> {
            hideKeyboard();
            showCarTypePop();
        });
    }

    private void sumbit() {
        if (TextUtils.isEmpty(binding.tvName.getText())) {
            MToast.showToast(this, "请输入姓名");
            return;
        }
        if (TextUtils.isEmpty(binding.tvId.getText())) {
            MToast.showToast(this, "请输入身份证号");
            return;
        }
        if (TextUtils.isEmpty(binding.tvCarNum.getText())) {
            MToast.showToast(this, "请输入车牌号");
            return;
        }
        if (TextUtils.isEmpty(binding.tvCarLengh.getText())) {
            MToast.showToast(this, "请选择车长");
            return;
        }
        if (TextUtils.isEmpty(binding.tvCarModel.getText())) {
            MToast.showToast(this, "请选择车型");
            return;
        }
        if (TextUtils.isEmpty(binding.tvCarWeight.getText())) {
            MToast.showToast(this, "请输入额定载重");
            return;
        }

    }



    //获取InputMethodManager，隐藏软键盘
    private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘
        imm.hideSoftInputFromWindow(this.getWindow().getDecorView().getWindowToken(), 0);
    }

    //车长选择器
    private void showCarLengthTypePop() {
        OptionsPickerView carLengthTypeOption = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                carLenNO = carsLengthData.get(options1).getNo();
                String content = carsLengthData.get(options1).getName();
                goodsInfoTypeEntity3.setName(carsLengthData.get(options1).getName());
                binding.tvCarLengh.setText(content);
                carLengthTypePosition = options1;
            }
        }).setDecorView(getWindow().getDecorView().findViewById(android.R.id.content)).build();
        if (carsLengthData != null && carsLengthData.size() > 0) {
            carLengthTypeOption.setPicker(carsLengthData);
            carLengthTypeOption.setSelectOptions(carLengthTypePosition);
            carLengthTypeOption.show();
        } else {
            MToast.showToast(getActivity(), "后台无数据！");
        }
    }

    //车类型选择器
    private void showCarTypePop() {
        OptionsPickerView carTypeOption = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                carTypeNO = carsTypeData.get(options1).getNo();
                String content = carsTypeData.get(options1).getName();
                goodsInfoTypeEntity3.setName(carsTypeData.get(options1).getName());
                binding.tvCarModel.setText(content);
                carTypePosition = options1;
            }
        }).setDecorView(getWindow().getDecorView().findViewById(android.R.id.content)).build();
        if (carsTypeData != null && carsTypeData.size() > 0) {
            carTypeOption.setPicker(carsTypeData);
            carTypeOption.setSelectOptions(carTypePosition);
            carTypeOption.show();
        } else {
            MToast.showToast(getActivity(), "后台无数据！");
        }
    }



    @Override
    protected void init() {
        viewModel.getDriverAuthDetail();
    }

    //  status  这个是审核状态 0 待审核 1 审核通过 2 审核不通过
    //  state   0  下架  1上架

    /*  司机审核通过/审核不通过/已下架，都可以修改审核信息
        点击跳转编辑页面，讲当前信息带入变更为可修改状态
        提交后司机审核状态变为审核中，前台的报价、下单等操作都不可进行，提示需要审核通过才能操作。*/

    public void showDetails(DriverAuthDetailEntity data) {
        if (data.getStatus() == 0) {
            binding.tvStatus.setText("审核中");
            binding.tvStatus.setTextColor(getResources().getColor(R.color.color_4A8BF0));
            binding.llStatusDetails.setVisibility(View.GONE);
            binding.include.rightView.setVisibility(View.GONE);
        } else if (data.getStatus() == 1) {
            binding.tvStatus.setText("审核通过");
            binding.tvStatus.setTextColor(getResources().getColor(R.color.color_green_00d3a9));
            binding.llStatusDetails.setVisibility(View.GONE);
            binding.include.rightView.setVisibility(View.VISIBLE);
        } else if (data.getStatus() == 2) {
            binding.tvStatus.setText("审核不通过");
            binding.tvStatus.setTextColor(getResources().getColor(R.color.red));
            binding.llStatusDetails.setVisibility(View.VISIBLE);
            binding.include.rightView.setVisibility(View.VISIBLE);
            binding.tvStatusDetails.setText("您的信息提交不完整，请重新提交审核，通过后方可正常使用！");
            if (data.getState() == 0) {
                binding.tvStatus.setText("已下架");
                binding.tvStatus.setTextColor(getResources().getColor(R.color.goldenrodColor));
                binding.llStatusDetails.setVisibility(View.VISIBLE);
                binding.tvStatusDetails.setText("您的信息存在违规，请重新提交审核，通过后方可正常使用！");
            }
        }

        binding.tvName.setText(data.getName());
        binding.tvId.setText(data.getIdCard());
        binding.tvCarNum.setText(data.getCarNumber());
        binding.tvPosition.setText(data.getCityName());
        binding.tvCarLengh.setText(data.getCarLenName());
        binding.tvCarModel.setText(data.getCarTypeName());
        binding.tvCarWeight.setText(String.valueOf(data.getWeight()));


    }
}
